<?php
/**
 * Copyright (c) 2006-2008, Julien PORTALIER
 * http://featherphp.googlecode.com/
 * 
 * Licensed under The MIT License
 * Redistributions of files must retain the above copyright notice.
 */

class TranslateScript extends ApplicationScript
{
	public $auto_render = false;
	
	function main()
	{
		
	}
	
	function extract_all()
	{
		require_once 'libs/files.php';
		
		# extracts strings from source files
		$files = array();
		files::find(APP,     'php|tpl', &$files);
		files::find(FEATHER, 'php|tpl', &$files);
		
		$translations = $this->_parse($files);
		foreach($translations as $ctx => $messages)
		{
			ob_start();
			echo '# SOME DESCRIPTIVE TITLE.
# Copyright (C) YEAR THE PACKAGE\'S COPYRIGHT HOLDER
# This file is distributed under the same license as the PACKAGE package.
# FIRST AUTHOR <EMAIL@ADDRESS>, YEAR.
#
#, fuzzy
msgid ""
msgstr ""
"Project-Id-Version: PACKAGE VERSION\n"
"Report-Msgid-Bugs-To: \n"
"POT-Creation-Date: '.date('Y-m-d H:iO').'\n"
"PO-Revision-Date: YEAR-MO-DA HO:MI+ZONE\n"
"Last-Translator: FULL NAME <EMAIL@ADDRESS>\n"
"Language-Team: LANGUAGE <LL@li.org>\n"
"MIME-Version: 1.0\n"
"Content-Type: text/plain; charset='.Core::charset.'"\n"
"Content-Transfer-Encoding: 8bit\n"

';
			foreach($messages as $msgid)
			{
				if (is_array($msgid))
				{
					# plural form
					echo 'msgid: "'.str_replace('"', '\"', $msgid[0]).'"'.LF;
					echo 'msgid_plural: "'.str_replace('"', '\"', $msgid[1]).'"'.LF;
					echo 'msgstr[0]: ""'.LF;
					echo 'msgstr[1]: ""'.LF;
				}
				else
				{
					# singular form
					echo 'msgid: "'.str_replace('"', '\"', $msgid).'"'.LF;
					echo 'msgstr: ""'.LF;
				}
				echo LF;
			}
			$content = ob_get_clean();
			
			# stores messages for context in a POT file
			file_put_contents(APP.'locales'.DS.$ctx.'.pot');
		}
	}
	
	private function & _parse(array &$files)
	{
		$re_parameters        = '(?:[\s,]*[\"](?:[^\"]+)[\"][\s,]*|[\s,]*[\'](?:[^\']+)[\'][\s,]*|[\s,\$\w\_\-]|[\s,]+)+';
		$re_functions         = 't|tn|translate::singular|translate::plural';
		$re_extract_functions = '/\b('.$re_functions.')\s*\(('.$re_parameters.')\)/s';
		$split_parameters     = '/[\s,]*[\"]([^\"]+)[\"][\s,]*|[\s,]*[\']([^\']+)[\'][\s,]*|[\s,]+/';
		$translations         = array();
		
		foreach($files as $path)
		{
			# read file content and removes comments
			$content = file_get_contents($path);
			$content = preg_replace(array(':(#|//)(.*):', ':/\*(.+?)\*/:s'), '', $content);
			
			# extracts PHP code
			preg_match_all('/<([%?])(?:php|)(.+?)\1>/s', $content, $phps, PREG_SET_ORDER);
			foreach($phps as $php)
			{
				# extracts translation functions from PHP code
				preg_match_all($re_extract_functions, $php[2], $messages, PREG_SET_ORDER);
				
				foreach($messages as $mesg)
				{
					# parses function arguments (careful of commas, quotes and single quotes)
					$args = preg_split($split_parameters, trim($mesg[2]), 0,
						PREG_SPLIT_NO_EMPTY | PREG_SPLIT_DELIM_CAPTURE);
					
					# singular translation
					if ($mesg[1] == 't' or $mesg[1] == 'translate::singular')
					{
						if (strpos($args[0], '$') === 0) {
							continue;
						}
						$ctx = isset($args[1]) ? $args[1] : 'global';
						$translations[$ctx][] = $args[0];
					}
					
					# plural translation
					elseif ($mesg[1] == 'tn' or $mesg[1] == 'translate::plural')
					{
						if (strpos($args[0], '$') === 0 or strpos($args[1], '$') === 0) {
							continue;
						}
						$ctx = isset($args[3]) ? $args[3] : 'global';
						$translations[$ctx][] = array($args[0], $args[1]);
					}
					
					# shouldn't happen
					else {
						trigger_error("Unknown translation function: $mesg.", E_USER_WARNING);
					}
				}
			}
		}
		return $translations;
	}
}
?>